package com.library.payment.dao;

import com.framework.BaseDao;
import com.framework.Page;
import com.library.entity.Payment;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Repository
public class PaymentDaoImpl extends BaseDao<Payment,String> {
	@Resource
	private SessionFactory sessionFactory;

	/*查询缴费 分页*/
	public Page<Payment> findPageByProperty(int pageNum, int pageSize, Map paramsMap){
		int i = 0;
		int j = 0;
		String key;
		StringBuilder hql = new StringBuilder("from Payment p");
		List<Object> params = new ArrayList();

		if(paramsMap.size() != 0){
			Iterator<String> it = paramsMap.keySet().iterator();
			hql.append(" where ");
			while (it.hasNext()) {
				key = it.next();
				params.add(paramsMap.get(key));

				if(j != 0){
					if(params.get(i) != null)
						hql.append(" and p."+ key + "=?");
					else{
						hql.append(" and p."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				} else{
					if(params.get(i) != null)
						hql.append(" p."+ key + "=?");
					else{
						hql.append(" p."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}

				}
				++i;
				++j;
			}
		}
		try{
			return super.findByPage(pageNum,pageSize,hql.toString(),params.toArray());
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*查询缴费 不分页*/
	public List<Payment> findPaymentsByProperty( Map paramsMap){
		int i = 0;
		int j = 0;
		String key;
		StringBuilder hql = new StringBuilder("from Payment p");
		List<Object> params = new ArrayList();

		if(paramsMap.size() != 0){
			Iterator<String> it = paramsMap.keySet().iterator();
			hql.append(" where ");
			while (it.hasNext()) {
				key = it.next();
				params.add(paramsMap.get(key));

				if(j != 0){
					if(params.get(i) != null)
						hql.append(" and p."+ key + "=?");
					else{
						hql.append(" and p."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				} else{
					if(params.get(i) != null)
						hql.append(" p."+ key + "=?");
					else{
						hql.append(" p."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}

				}
				++i;
				++j;
			}
		}
		try{
			return super.findByProperty(hql.toString(),params.toArray());
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public Payment getPaymentById(Integer paymentId){
		try{
			return super.get(paymentId);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*查找条件查找总费用*/
	public Double findCostByProperty(Map paramsMap){
		int i = 0;
		int j = 0;
		String key;
		StringBuilder hql = new StringBuilder("from Payment p");
		List<Object> params = new ArrayList();
		Double sumMoney;

		if(paramsMap.size() != 0){
			Iterator<String> it = paramsMap.keySet().iterator();
			hql.append(" where ");
			while (it.hasNext()) {
				key = it.next();
				params.add(paramsMap.get(key));

				if(j != 0){
					if(params.get(i) != null){
						if(key.equals("date")){
							hql.append(" and p."+ key + "like ?%");
						}else{
							hql.append(" and p."+ key + "=?");
						}
					}

					else{
						hql.append(" and p."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				} else{
					if(params.get(i) != null){
						if(key.equals("date")){
							hql.append(" p."+ key + "like ?%");
						}else{
							hql.append(" p."+ key + "=?");
						}
					}
					else{
						hql.append(" p."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				}
				++i;
				++j;
			}
		}
		try{
			sumMoney = this.getPaymentsCost("select sum(money) " + hql.toString(),params.toArray());
			if(sumMoney == null){
				return 0.0;
			}else
				return sumMoney;
		}catch(Exception e){
			e.printStackTrace();
			return 0.0;
		}
	}

	/*按条件查询费用*/
	public Double getPaymentsCost(String hql,Object[] params){
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++)
				query.setParameter(i, params[i]);
		}else{
			return 0.0;
		}
		return (Double) query.uniqueResult();
	}

	/*查找条件数量*/
	public Long findCountByProperty(Map paramsMap){
		int i = 0;
		int j = 0;
		String key;
		StringBuilder hql = new StringBuilder("from Payment p");
		List<Object> params = new ArrayList();

		if(paramsMap.size() != 0){
			Iterator<String> it = paramsMap.keySet().iterator();
			hql.append(" where ");
			while (it.hasNext()) {
				key = it.next();
				params.add(paramsMap.get(key));

				if(j != 0){
					if(params.get(i) != null)
						hql.append(" and p."+ key + "=?");
					else{
						hql.append(" and p."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				} else{
					if(params.get(i) != null)
						hql.append(" p."+ key + "=?");
					else{
						hql.append(" p."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}

				}
				++i;
				++j;
			}
		}
		try{
			return this.getPaymentsCount(hql.toString(),params.toArray());
		}catch(Exception e){
			e.printStackTrace();
			return (long)0;
		}
	}

	/*按条件查询数量*/
	public Long getPaymentsCount(String hql,Object[] params){
		try{
			return super.findCountByPage("select count(*) " + hql,params);
		}catch(Exception e){
			e.printStackTrace();
			return (long)0;
		}
	}

	public void savePayment(Payment payment){
		try {
			this.save(payment);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updatePayment(Payment payment){
		try {
			this.update(payment);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void deletePayment(Payment payment){
		try {
			this.delete(payment);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
